package gui;

import boardgame.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.SwingConstants;
import javax.swing.border.*;

public abstract class BoardGUI {		
	
	protected int rows = 8;
	protected int cols = 8;
	protected int depth = 1;
	protected GameState model = null;	
	protected JFrame mainFrame = new JFrame("Game Window");
	public JLabel statusBar = new JLabel("Game Start!");
	
	public BoardGUI(){
		
	}
	
	public BoardGUI(int r, int c, int d){
		rows = r;
		cols = c;
		setDepth(d);
	}
	
    public void setRows(int rows) {
		this.rows = rows;
	}

	public int getRows() {
		return rows;
	}

	public void setCols(int cols) {
		this.cols = cols;
	}

	public int getCols() {
		return cols;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	public int getDepth() {
		return depth;
	}

	public JPanel createDefaultGrid() {
    	JPanel grid = new JPanel(); 
    	grid.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        for (int i = 0; i < rows; i++){
        	c.gridx = 0;
        	for (int j = 0; j < cols; j++){
        		int[] id = {i, j, 1};
        		GButton button = new GButton(id);        		
        		grid.add(button,c);
        		c.gridx++;
        	}
        	c.gridy++;
        	System.out.println(c.gridy);
        }    
        if(rows >= 10 && cols >= 10)
        	grid.setSize(500, 500);
        else if (rows >= 10)
        	grid.setSize(500, 50*cols);
        else if (cols >= 10)
        	grid.setSize(50*rows, cols);
        else
        	grid.setSize(50*rows, 50*cols);
        return grid;
    }
    
    public void setDefaultWindow(){
    	JFrame.setDefaultLookAndFeelDecorated(true);        
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        JPanel contents = new JPanel();
        contents.setLayout(new GridBagLayout());        
        statusBar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        GridBagConstraints c = new GridBagConstraints();
        //c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;	
        c.gridy = 0;        
        contents.add(createGrid(), c);
        c.fill = GridBagConstraints.HORIZONTAL;        
        c.gridy++;        
        c.anchor = GridBagConstraints.PAGE_END;
        c.gridwidth = 2;
        statusBar.setHorizontalAlignment(SwingConstants.CENTER);
        contents.add(statusBar, c);        
        mainFrame.getContentPane().add(contents);
        mainFrame.setJMenuBar(createJMenuBar());
        mainFrame.pack();
    }
    
    public void showWindow(){
    	mainFrame.setVisible(true);
    }
    
    protected abstract void setupWindow();
    protected abstract JPanel createGrid(); //can use layered panes later
    protected abstract JMenuBar createJMenuBar();
    protected abstract void setModel(GameState model);
    public abstract void clear(int win);
    public abstract void reset();
    public abstract void update(Object object);
    public abstract void updateStatus(String status);
}
